
#include <BLabel>
#include <BVBoxLayout>
#include <BHBoxLayout>
#include <BTableItem>

#include "StringUtils.h"
#include "LayoutPanel.h"

LayoutPanel::LayoutPanel(BWidget* parent) : BGroupBox(parent)
{
	BGridLayout* gridLayout = new BGridLayout();
	{
		BLabel* nameLabel = new BLabel("name");
		_nameEdit = new BLineEdit();

		_enabledCheckBox = new BCheckBox("enabled");
		_splitableCheckBox = new BCheckBox("splitable");

		BLabel* marginLabel = new BLabel("margin");
		_marginEdit = new BLineEdit();

		BLabel* partLabel = new BLabel("part");
		_partComboBox = new BComboBox();
		_partComboBox->addItem("None", Part_None);
		_partComboBox->addItem("Top", Part_Top );
		_partComboBox->addItem("Left", Part_Left);
		_partComboBox->addItem("Right", Part_Right);
		_partComboBox->addItem("Bottom", Part_Bottom);
		_partComboBox->addItem("Center", Part_Center);

		gridLayout->addWidget(0, 0, nameLabel);
		gridLayout->addWidget(0, 1, _nameEdit);
		gridLayout->addWidget(1, 0, _enabledCheckBox);
		gridLayout->addWidget(1, 1, _splitableCheckBox);
		gridLayout->addWidget(2, 0, marginLabel);
		gridLayout->addWidget(2, 1, _marginEdit);
	}

	_policyMenu = new PolicyMenu();
	_stretchSpinBox = new BFloatSpinBox();

	BVBoxLayout* vlayout = new BVBoxLayout(this);
	vlayout->addLayout(gridLayout);

	connect(this, Signal_AssetChanged, &LayoutPanel::slotAssetChanged);

	connect(_nameEdit, Signal_EditFinished, &LayoutPanel::slotNameEditFinished);
	connect(_enabledCheckBox, Signal_Toggled, &LayoutPanel::slotEnabledToggled);
	connect(_splitableCheckBox, Signal_Toggled, &LayoutPanel::slotSplitableToggled);
	connect(_marginEdit, Signal_EditFinished, &LayoutPanel::slotMarginEditFinished);

	connect(_policyMenu, Signal_Triggered, &LayoutPanel::slotPolicyMenuTrigger);
	connect(_stretchSpinBox, Signal_EditFinished, &LayoutPanel::slotStretchEditFinished);
}

void LayoutPanel::slotAssetChanged(BObject* object, const BValue& value)
{
	if (BLayout* layout = value)
	{
		_nameEdit->setText(layout->name());
		_enabledCheckBox->setChecked(layout->enabled());
		_marginEdit->setText(StringFromMargin(layout->margin()));
	}
}

void LayoutPanel::slotNameEditFinished(BObject* object, const BValue& value)
{
	const BString& name = value;
	BLayout* layout = this->asset();
	layout->setName(name);
}
void LayoutPanel::slotEnabledToggled(BObject* object, const BValue& value)
{
	bool enabled = value;
	BLayout* layout = this->asset();
	layout->setEnabled(enabled);
}
void LayoutPanel::slotSplitableToggled(BObject* object, const BValue& value)
{
	bool splitable = value;
	BLayout* layout = this->asset();
	if (BVBoxLayout* vlayout = this->asset())
	{
		vlayout->setSplitable(splitable);
	}
	if (BHBoxLayout* hlayout = this->asset())
	{
		hlayout->setSplitable(splitable);
	}
	if (BGridLayout* gridlayout = this->asset())
	{
		gridlayout->setSplitable(splitable);
	}
}
void LayoutPanel::slotMarginEditFinished(BObject* object, const BValue& value)
{
	const BString& text = value;
	BMargin margin = StringToMargin(text);
	BLayout* layout = this->asset();
	layout->setMargin(margin);
}
void LayoutPanel::slotPartItemClicked(BObject* object, const BValue& value)
{
	BComboItem* item = value;
	PartFlag part = item->value();
	if (BLayout* layout = this->asset())
	{
		layout->host()->setLayout(layout, part);
	}
}

void LayoutPanel::slotPolicyMenuTrigger()
{
	BTableItem* item = _policyMenu->asset();
	item->setText(_policyMenu->title());
	item->setAsset(_policyMenu->policy());
}
void LayoutPanel::slotStretchEditFinished()
{
	BTableItem* item = _stretchSpinBox->asset();
	item->setText(_stretchSpinBox->value());
	_stretchSpinBox->hide();
}
